package edu.nju.cineplex.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import edu.nju.cineplex.dao.DaoHelper;
import edu.nju.cineplex.dao.HallDao;
import edu.nju.cineplex.model.Hall;

public class HallDaoImpl implements HallDao {
	private static HallDaoImpl hallDao = new HallDaoImpl();
	private static DaoHelper daoHelper = DaoHelperImpl.getBaseDaoInstance();

	private HallDaoImpl() {

	}

	public static HallDaoImpl getInstance() {
		return hallDao;
	}

	@Override
	public Hall findById(Long id) {
		// TODO Auto-generated method stub
		Connection con = daoHelper.getConnection();
		PreparedStatement stmt = null;
		ResultSet result = null;
		try {
			stmt = con.prepareStatement("select * from hall where id = ?");
			stmt.setLong(1, id);
			result = stmt.executeQuery();
			if (result.next()) {
				Hall hall = new Hall();
				hall.setId(result.getLong(1));
				hall.setType(result.getString(2));
				hall.setSeatNum(result.getInt(3));
				return hall;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
			daoHelper.closeResultSet(result);
		}
		return null;
	}

	@Override
	public List<Hall> findAllHall() {
		// TODO Auto-generated method stub
		Connection con = daoHelper.getConnection();
		PreparedStatement stmt = null;
		ResultSet result = null;
		List<Hall> allHall = new ArrayList<Hall>();
		try {
			stmt = con.prepareStatement("select * from hall");
			result = stmt.executeQuery();
			while (result.next()) {
				Hall hall = new Hall();
				hall.setId(result.getLong(1));
				hall.setType(result.getString(2));
				hall.setSeatNum(result.getInt(3));
				allHall.add(hall);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
			daoHelper.closeResultSet(result);
		}
		return allHall;
	}

}
